package com.hb.znpt.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.hb.znpt.BR;
import com.hb.znpt.R;
import com.hb.znpt.adapter.OfflineAdapter;
import com.hb.znpt.bean.UpdataBean;
import com.hb.znpt.databinding.ActivityOfflineBinding;
import com.hb.znpt.utils.Constants;
import com.hb.znpt.viewmodel.OfflineViewModel;
import com.unitlib.base.base.BaseActivity;
import com.unitlib.base.utils.SPUtils;

import java.util.List;

public class OfflineActivity extends BaseActivity<ActivityOfflineBinding, OfflineViewModel> {

    private OfflineAdapter adapter;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_offline;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        initBackTitle(binding.toolbar);
        initRecyclerView();
    }

    @Override
    public void initViewObservable() {
        viewModel.listData.observe(this, new Observer<List<UpdataBean>>() {
            @Override
            public void onChanged(List<UpdataBean> beans) {
                adapter.setList(beans);
            }
        });
    }

    /**
     * Init recycler view.
     * 初始化RecyclerView
     */
    public void initRecyclerView() {
        adapter = new OfflineAdapter();
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAdapter(adapter);
        adapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                UpdataBean bean = (UpdataBean) adapter.getItem(position);
                if (view.getId() == R.id.deleteBtn) {
                    AlertDialog.Builder normalDialog = new AlertDialog.Builder(context);
                    normalDialog.setMessage("是否要删除这条记录?")
                            .setPositiveButton("删除", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    viewModel.delete(bean);
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            }).show();
                } else if (view.getId() == R.id.updataBtn) {
                    viewModel.updataImg(bean);
                }
            }
        });
    }
}
